//
//  BadgeView.m
//  LotteryClient
//
//  Created by Suny on 14-7-5.
//  Copyright (c) 2014年 CTTC. All rights reserved.
//

#import "BadgeView.h"
#import "UIImage+BaseFramework.h"
@interface BadgeView ()
{
    BOOL        _isKVOAdded;
}
@property (nonatomic, readwrite, assign) SYBadgeViewType badgeType;
@end

@implementation BadgeView
#pragma mark life cycle

- (id)initWithImage:(UIImage *)image
{
    self = [self initWithImage:image badgeType:BadgeViewIcon];
    if (self) {
        
    }
    
    return self;
}

- (id)initWithImage:(UIImage *)image badgeType:(SYBadgeViewType)badgeType
{
    self = [self initWithImage:image badgeType:badgeType autoStrech:NO];
    if (self) {
        
    }
    return self;
}

- (id)initWithImage:(UIImage *)image badgeType:(SYBadgeViewType)badgeType autoStrech:(BOOL)autoStrech
{
    UIImage *newImage = (autoStrech) ? [image stretchableImageByCenter] : image;
    self = [super initWithImage:newImage];
    if (self) {
        self.badgeType = badgeType;
		//        self.hidden = YES;
        _autoStrech = autoStrech;
        _defaultImageSize = newImage.size;
        _hidesWhenNoText = YES;
        _textLabelInsets = UIEdgeInsetsMake(0, 5, 0, 5);
        
        _maxHeight = 80;
        _maxWidth = 100;
        _maxNumber = 99;
        
        if (BadgeViewIconWithText == self.badgeType)
        {
            _textLabel = [[UILabel alloc] initWithFrame:self.bounds];
            _textLabel.backgroundColor = [UIColor clearColor];
            _textLabel.font = [UIFont systemFontOfSize:11];
            _textLabel.textAlignment = NSTextAlignmentCenter;
            [self addSubview:_textLabel];
            
            [self addTextLabelKVO];
        }
    }
    
    return self;
}

- (void)dealloc
{
    [self removeTextLabelKVO];

}

#pragma mark KVO
- (void)addTextLabelKVO
{
    if (!_isKVOAdded && _textLabel)
    {
        [_textLabel addObserver:self forKeyPath:@"text" options:NSKeyValueObservingOptionNew context:NULL];
        [_textLabel addObserver:self forKeyPath:@"font" options:NSKeyValueObservingOptionNew context:NULL];
        _isKVOAdded = YES;
    }
}

- (void)removeTextLabelKVO
{
    if (_isKVOAdded)
    {
        [_textLabel removeObserver:self forKeyPath:@"text"];
        [_textLabel removeObserver:self forKeyPath:@"font"];
        _isKVOAdded = NO;
    }
}

#pragma mark
- (void)showBadgeWithoutText
{
    _textLabel.text = @"";
    self.hidden = NO;
}

- (void)setMaxHeight:(CGFloat)maxHeight
{
    _maxHeight = MAX(_defaultImageSize.height, maxHeight);
}

- (void)setMaxWidth:(CGFloat)maxWidth
{
    _maxWidth = MAX(_defaultImageSize.width, maxWidth);
}

- (void)setBadgeImage:(UIImage *)badgeImage autoStrech:(BOOL)autoStrech
{
    UIImage *newImage = (autoStrech) ? [badgeImage stretchableImageWithLeftCapWidth:badgeImage.size.width/2 topCapHeight:badgeImage.size.height/2] : badgeImage;
    self.image = newImage;
    
    _autoStrech = autoStrech;
}

- (void)setBadgeText:(NSString *)text
{
    _textLabel.text = text;
    
    if (_hidesWhenNoText && text == nil)
	{
        self.hidden = YES;
    }
    else
	{
        self.hidden = NO;
    }
}

- (void)setBadgeNumber:(NSInteger)number
{
    if (number <= 0) 
	{
        _textLabel.text = nil;
    }
    else if (number <= _maxNumber)
    {
        _textLabel.text = [NSString stringWithFormat:@"%ld", (long)number];
    }
    else 
	{
        _textLabel.text = [NSString stringWithFormat:@"%lu+", (unsigned long)_maxNumber];
    }
    
    NSString *text = _textLabel.text;
    
    if (_hidesWhenNoText && text == nil) 
	{
        self.hidden = YES;
    }
    else 
	{
        self.hidden = NO;
    }
}

- (void)setTextLabelEdgeInsets:(UIEdgeInsets)insets
{
    _textLabelInsets = insets;
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    NSString *text = _textLabel.text;
    
    CGRect frame = self.frame;
    CGRect textFrame = _textLabel.frame;
    if (text && _autoStrech) {
        CGSize size = [text sizeWithFont:_textLabel.font];
        size.height = size.height + _textLabelInsets.top + _textLabelInsets.bottom;
        size.width = size.width + _textLabelInsets.left + _textLabelInsets.right;
        
        size.width = MAX(_defaultImageSize.width, size.width);
        size.height = MAX(_defaultImageSize.height, size.height);
        
        size.width = MIN(size.width, _maxWidth);
        size.height = MIN(size.height, _maxHeight);
        
        frame.origin.x = CGRectGetMaxX(frame)-size.width;
        frame.size = CGSizeMake(size.width, size.height);
        
        textFrame = CGRectMake(0, 0, size.width, size.height);
        textFrame.origin.y = CGRectGetHeight(frame)/2 - CGRectGetHeight(textFrame)/2;
        textFrame.origin.x = CGRectGetWidth(frame)/2 - CGRectGetWidth(textFrame)/2;
        
    }
    else {
        frame.origin.x = CGRectGetMaxX(frame)-_defaultImageSize.width;
        frame.size = _defaultImageSize;
        textFrame = CGRectMake(0, 0, CGRectGetWidth(frame), CGRectGetHeight(frame));
    }
    
    _textLabel.frame = textFrame;
    self.frame = frame;
}

#pragma mark - KVO

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
	[self setNeedsLayout];
}

@end